#****************************************************************************
# Copyright (C) 2001-2004  PEAK System-Technik GmbH
#
# linux@peak-system.com
# www.peak-system.com
#
# Maintainer(s): Klaus Hitschler (klaus.hitschler@gmx.de)
#****************************************************************************

#****************************************************************************
#
# makefile for Peak CAN interface drivers (ISA, PCI, DONGLE, USB)
# up to kernels less or equal than 2.4.x
# with obfuscation for USB kernel only
#
# $Log: Makefile-2.4,v $
# Revision 1.5  2004/05/08 10:18:21  klaus
# Added test for file version.h prior to resolve version from this file
#
# Revision 1.4  2004/05/07 17:35:54  klaus
# Corrected VERSION retrival for Mandrake and RedHats version.h
#
# Revision 1.3  2004/05/02 15:56:51  klaus
# Last corrections and improvements, especially for Makefile(s) and build scenarios
#
# Revision 1.2  2004/05/02 13:25:50  klaus
# Little improvements, corrections
#
# Revision 1.1  2004/05/02 12:06:01  klaus
# total rebuild of Makefile for KBUILD support for Kernel 2.6
#
#****************************************************************************

#****************************************************************************
# targets:
# depend	    creates dependencies from included header files
# all		      does a standard make of the driver
# clean		    cleans up, base for a forced new make
# install	    run as root, installs the drivers
# rpminstall	only for rpm installation, do not run interactively
# obfuscate	  only for Peak System-Technik use
#
# make options:
# DBG=DEBUG                 or NO_DEBUG    		  switch debug on
# KERNEL_LOCATION=...       where your kernel sources are located if no standard location (cross-compile)
# MOD=NO_MODVERSIONS		    or MODVERSIONS 		  compile for modversions use
# PAR=NO_PARPORT_SUBSYSTEM	or PARPORT_SUBSYSTEM 	compile for use without parport subsystem und pcan-dongle
# USB=NO_USB_SUPPORT 		    or USB_SUPPORT    	compile for use with USB and pcan-usb
# DEVFS=NO_DEVFS_SUPPORT    or DEVFS_SUPPORT    compile to support device file system
# ORG=NO                    or YES              take always NO, only for Peak System-Technik use
# KERNEL_LOCATION=...       where your kernel sources are located if no standard location (cross-compile)
#
# please the first time do a make clean; make
#

#****************************************************************************
# please modify only here if your kernel path is elsewhere located
#
KERNEL_LOCATION = /usr/src/linux

#****************************************************************************
# whole makefile is based on availability of version.h file
# do a forced stop if it is not available
#
HASVERSION_H := $(shell if test -f $(KERNEL_LOCATION)/include/linux/version.h ; then echo yes ; else echo no; fi )

ifeq ($(HASVERSION_H),no)
$(error "Can't find $(KERNEL_LOCATION)/include/linux/version.h !")
else

#****************************************************************************
# removed old version retrival for better cross compile support
# old VERSION := $(shell uname -r)
#
VERSION := $(shell cpp -dM -I$(KERNEL_LOCATION)/include $(KERNEL_LOCATION)/include/linux/version.h \
            | grep UTS_RELEASE | sed -e 's;[^"]*"\(.*\)";\1;g')

#****************************************************************************
# get extracted kernel VERSION and PATCHLEVEL for comparison
#
KVERSION    := $(shell echo $(VERSION) | sed -e 's;\([1-9]\)\..*;\1;g' )
KPATCHLEVEL := $(shell echo $(VERSION) | sed -e 's;[1-9]\.\([0-9]\{0,3\}\)\..*;\1;g' )

#****************************************************************************
# removed old kernel source path retrival for support of kernels < 2.2.18!
# old KERNSRC = /lib/modules/$(VERSION)/build
#
KERNSRC := $(KERNEL_LOCATION)

#****************************************************************************
# where the driver should be installed - change here for cross install
#
INSTALL_LOCATION = /lib/modules/$(VERSION)/misc

#****************************************************************************
# some common switches and defines
#
PARMS   = type=isa,sp         # only for installer: please modify the install parameters according to your configuration
DBG     = NO_DEBUG
MOD     = MODVERSIONS
PAR     = PARPORT_SUBSYSTEM
USB     = USB_SUPPORT
DEVFS   = NO_DEVFS_SUPPORT
ORG     = NO                  # default custom make

OBF     = obfuscate
DEP     = .depend
INC     = $(KERNSRC)/include
SRC     = src
TEMP    = /tmp

CC      = gcc
LD      = ld

#****************************************************************************
# for local installation only: sometimes /etc/modules.conf.local is used
#
MODCONF = /etc/modules.conf
MODINST = "post-install pcan /usr/local/bin/pcan_make_devices 2"

#****************************************************************************
# standard TARGET up to kernel 2.4.x
#
TARGET    = pcan.o

CFLAGS  = -O2 -D__KERNEL__ -DMODULE -Wall -I$(INC) -I. -D$(DBG) -D$(MOD) -D$(PAR) -D$(USB) -D$(DEVFS)

ifeq ($(USB),USB_SUPPORT)
ifeq ($(ORG),YES)
FILES = $(SRC)/pcan_main.o $(SRC)/pcan_fops.o $(SRC)/pcan_pci.o $(SRC)/pcan_isa.o $(SRC)/pcan_dongle.o $(SRC)/pcan_fifo.o $(SRC)/pcan_sja1000.o $(SRC)/pcan_parse.o $(SRC)/pcan_usb.o $(SRC)/pcan_usb_kernel_org.o
else
FILES = $(SRC)/pcan_main.o $(SRC)/pcan_fops.o $(SRC)/pcan_pci.o $(SRC)/pcan_isa.o $(SRC)/pcan_dongle.o $(SRC)/pcan_fifo.o $(SRC)/pcan_sja1000.o $(SRC)/pcan_parse.o $(SRC)/pcan_usb.o $(SRC)/pcan_usb_kernel.o
endif
else
FILES = $(SRC)/pcan_main.o $(SRC)/pcan_fops.o $(SRC)/pcan_pci.o $(SRC)/pcan_isa.o $(SRC)/pcan_dongle.o $(SRC)/pcan_fifo.o $(SRC)/pcan_sja1000.o $(SRC)/pcan_parse.o
endif


#--- standard make path ---
all: message $(TARGET)

$(TARGET)  : $(FILES)
	$(LD) -r $^ -o $@

.PHONY : message
message:
	@ echo "***"
	@ echo "*** Host machine kernel version=$(shell uname -r), Driver kernel version=$(VERSION), Path of kernel sources=$(KERNSRC)"
	@ echo "***"

#--- make path for obfuscation ---
.PHONY : obfuscate
obfuscate:
	sed -e /COBF/,\$$d  $(SRC)/cobf.h             > $(TEMP)/cobf.h.header;\
	sed -e  /COBF/,\$$d  $(SRC)/pcan_usb_kernel.c > $(TEMP)/pcan_usb_kernel.c.header;\
	cobf -p $(OBF)/pp_gnu -o $(TEMP) -c pcan_usb_kernel.c -t $(OBF)/tlist.tok -m $(OBF)/mlist.tok -x pcan_ $(SRC)/pcan_usb_kernel_org.c;\
	cat $(TEMP)/pcan_usb_kernel.c.header  $(TEMP)/pcan_usb_kernel.c  > $(SRC)/pcan_usb_kernel.c;\
	cat $(TEMP)/cobf.h.header $(TEMP)/cobf.h > $(SRC)/cobf.h


#********** catch include file depencies ************************************
ifeq ($(DEP),$(wildcard $(DEP)))

depend:
	makedepend -f$(DEP) -- $(CFLAGS) -- $(addsuffix .c, $(basename $(FILES))) -I$(INC)

include $(DEP)

else

depend:
	touch $(DEP)
	makedepend -f$(DEP) -- $(CFLAGS) -- $(addsuffix .c, $(basename $(FILES))) -I$(INC)
endif

#********** clean all for a rebuild *****************************************
clean:
	rm -f $(SRC)/*.o *~ $(SRC)/*~ $(TARGET)
	
#********** this entry is reserved for root access only *********************
install:
	@if test -d $(INSTALL_LOCATION) ; then echo "info: $(INSTALL_LOCATION) exists."; else mkdir $(INSTALL_LOCATION); fi
	cp $(TARGET) $(INSTALL_LOCATION)/$(TARGET)
	cp pcan_make_devices /usr/local/bin/pcan_make_devices
	if test -z $(shell grep -l pcan $(MODCONF)); then\
		cp $(MODCONF) $(MODCONF)~;\
		echo ""                                               >> $(MODCONF);\
		echo "# pcan - automatic made entry, begin --------"  >> $(MODCONF);\
		echo "# options pcan $(PARMS)   # see documentation"  >> $(MODCONF);\
		echo $(MODINST)                                       >> $(MODCONF);\
		echo "# pcan - automatic made entry, end ----------"  >> $(MODCONF);\
		echo ""                                               >> $(MODCONF);\
	fi
	/sbin/depmod
	cp pcan.h /usr/include/pcan.h
	chmod 644 /usr/include/pcan.h


#********** dummy entry for rpm installation only ***************************
rpminstall:
	if test -d $(INSTALL_LOCATION) ; then echo "$(INSTALL_LOCATION) exists."; else mkdir $(INSTALL_LOCATION); fi
	cp $(TARGET) $(INSTALL_LOCATION)/$(TARGET)
	cp pcan_make_devices /usr/local/bin/pcan_make_devices
	cp pcan.h /usr/include/pcan.h
	chmod 644 /usr/include/pcan.h

#*********** end of Makefile ************************************************

endif  # <<<<< HASVERSION_H >>>>>>

# DO NOT DELETE
